﻿//fields
var defaultSearchOptions = {
    sopt: ['eq', 'ne', 'bw', 'bn', 'ew', 'en', 'cn', 'nc']
};
//var gridDefaultTimeout = 10000;
//$("[id*=lblError]").hide();
//jQuery.support.cors = true;


var controllerBaseUrl = 'Shipment';
$(document).ready(function () {

    $("#list").jqGrid({
        //IIS
        url: 'GetGridModel',
        datatype: 'json',
        //datatype: 'local',
        mtype: 'GET',
        colNames: ['Id', 'TrackingNo','State', 'Started', 'LastUpdate', 'Actions'],
        colModel: [
  { name: 'Id', index: 'Id', width: 30, align: 'left', hidden: true },
  { name: 'TrackingNumber', index: 'TrackingNumber', align: 'left', width: 100, searchoptions: defaultSearchOptions, sortable: true },
  { name: 'State', index: 'State', align: 'left', width: 100, searchoptions: defaultSearchOptions, sortable: true },
  { name: 'Started', index: 'Started', align: 'left', autowidth: true, searchoptions: defaultSearchOptions },
  { name: 'LastUpdate', index: 'LastUpdate', align: 'left', autowidth: true, searchoptions: defaultSearchOptions },
  { name: 'act', index: 'act', width: 150, sortable: false }
        ],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: "desc",
        viewrecords: true,
        caption: 'Functions',
        loadBeforeSend: function (jqXHR) {

        },
        autowidth: true,
        gridComplete: function () {
            $("[id*=lblError]").hide();

            var ids = jQuery("#list").jqGrid('getDataIDs');
            for (var i = 0; i < ids.length ; i++) {
                var cl = ids[i];
                var statusHistoryLink = '';
                var editLink = '';
                var deleteLink = '';
                var changeStateLink = '';
                var setupLink = '';

                var id = jQuery("#list").jqGrid('getRowData', cl).Id;

                deleteLink = CreateDeleteLink(id);

                changeStateLink = CreateChangeStateLink(id);
                jQuery("#list").jqGrid('setRowData', cl, { act: deleteLink + " " + changeStateLink });
            }

        },
        loadError: function (xhr, st, err) {
            jQuery("#rsperror").html("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText);
            alert(st + " " + err);
            $('#list').jqGrid('clearGridData');
            //$("[id=lblError]").show();
        },
        jsonReader: {
            repeatitems: false,
            page: function (obj) {
                return obj.page;
            },
            root: function (obj) {
                return obj.rows;
            },
            records: function (obj) {
                return obj.records;
            }
        }
    }).navGrid('#pager', { search: true, edit: false, add: false, del: false, searchtext: "Search", multipleSearch: true }
    , { multipleSearch: true }
    );
    //.jqGrid('searchGrid', { multipleSearch: true, overlay: false });

    $("#list").parents('div.ui-jqgrid-bdiv').css("height", "70%");
    $("[id*=lblError]").hide();

});


function AddTemplatedColumns(cl, isObsolete, columnName) {
    var checkedValue = '';
    if (isObsolete) {
        checkedValue = 'checked="checked"';
    }
    if (columnName == "IsEnabled") {
        jQuery("#list").jqGrid('setRowData', cl, { IsEnabled: '<input type="checkbox" disabled="disabled" ' + checkedValue + ' />' });
    }
    else if (columnName == "IsSetup") {
        jQuery("#list").jqGrid('setRowData', cl, { IsSetup: '<input type="checkbox" disabled="disabled" ' + checkedValue + ' />' });

    }
}

function CreateEditLink(id) {
    var hyperlinkEdit = controllerBaseUrl + 'Edit/' + id;
    var editLink = "<a href='" + hyperlinkEdit + "'>Edit</a>";
    return editLink;
}

function CreateDeleteLink(id) {
    var hyperlink = 'Delete/' + id;
    var editLink = "<a href='" + hyperlink + "'>Delete</a>";
    return editLink;
}
function CreateChangeStateLink(id) {
    var hyperlink = "Edit/" + id;

    var editLink = "<a href='" + hyperlink + "'> Change Status </a>";
    return editLink;
}